package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.LinkedList;
import java.util.ListIterator;

import butterknife.Bind;
import butterknife.ButterKnife;

import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseRecyclerAdapter;
import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.ui.Activity.User.UserCenterActivity;
import cn.kooki.app.duobao.util.StringUtils;
import cn.kooki.app.duobao.util.TimeUtils;
import cn.kooki.app.duobao.util.manager.CountDownObserver;
import cn.kooki.app.duobao.util.manager.CountDownTimerManager;

/**
 * Created by xiong on 2015/11/28.
 */
public class ResultAdapter extends BaseRecyclerAdapter<GoodsItem> implements CountDownObserver {


    private LinkedList<ReslutViewholder> viewholders = new LinkedList<>();
    private Context context;


    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_result_item, parent, false);
        ReslutViewholder viewholder = new ReslutViewholder(view);

        viewholders.add(viewholder);
        return viewholder;
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, final GoodsItem data) {
        ReslutViewholder viewholder = (ReslutViewholder) viewHolder;
        if (StringUtils.nullSafeParseInt(data.getStatus()) == 1) {
            viewholder.resultRevealingWrapper.setVisibility(View.VISIBLE);
            viewholder.resultRevealedWrapper.setVisibility(View.GONE);
        } else {
            viewholder.resultRevealingWrapper.setVisibility(View.GONE);
            viewholder.resultRevealedWrapper.setVisibility(View.VISIBLE);
        }
        viewholder.resultRevealedNum.setText(data.getCanyurenshu());

        viewholder.resultQihao.setText(data.getQishu());
        if (data.getNew_time() <= 0) {
            viewholder.resultRevealingCountdown.setVisibility(View.GONE);
            viewholder.resultRevealingText.setVisibility(View.VISIBLE);
        } else {

            viewholder.resultRevealingCountdown.setText(CountDownTimerManager.getInstance().transtime(data.getNew_time()));
            viewholder.resultRevealingText.setVisibility(View.GONE);
            viewholder.resultRevealingCountdown.setVisibility(View.VISIBLE);
        }
        viewholder.resultCountDownQihao.setText(data.getQishu());
        Glide.with(context).load(data.getThumb()).placeholder(R.drawable.img_blank).into(viewholder.resultGoodsPic);
        viewholder.resultGoodsName.setText(data.getTitle());
        viewholder.resultRevealedName.setText(data.getQ_user());
        viewholder.resultRevealedNum.setText(data.getQ_num());
        viewholder.resultRevealedCode.setText(data.getQ_user_code());
        viewholder.resultRevealedTime.setText(TimeUtils.getTime(data.getEndtime()));
        viewholder.resultRevealedWrapper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserCenterActivity.class);
                intent.putExtra("uid", data.getQ_uid());
                context.startActivity(intent);
            }
        });

        viewholder.goodTenLabel.setVisibility(data.getYunjiage().equals("10") ? View.VISIBLE : View.GONE);

    }

    public void startObservable() {
        CountDownTimerManager.getInstance().CountDownobservable.registerObserver(this);
    }


    private void checkTime(GoodsItem item) {
        ListIterator<ReslutViewholder> iterator = viewholders.listIterator();
        while (iterator.hasNext()) {
            ReslutViewholder viewholder = iterator.next();
            viewholder.setinfo(item);
        }
    }

    public void stopObservable() {
        CountDownTimerManager.getInstance().CountDownobservable.unregisterObserver(this);

    }


    @Override
    public void onChanged(GoodsItem item) {
        checkTime(item);
    }

    class ReslutViewholder extends Holder {

        @Bind(R.id.result_goods_pic)
        ImageView resultGoodsPic;
        @Bind(R.id.result_goods_name)
        TextView resultGoodsName;
        @Bind(R.id.result_qihao)
        TextView resultQihao;
        @Bind(R.id.result_revealed_name)
        TextView resultRevealedName;
        @Bind(R.id.result_revealed_num)
        TextView resultRevealedNum;
        @Bind(R.id.result_revealed_code)
        TextView resultRevealedCode;
        @Bind(R.id.result_revealed_time)
        TextView resultRevealedTime;
        @Bind(R.id.result_revealed_wrapper)
        LinearLayout resultRevealedWrapper;
        @Bind(R.id.result_count_down_qihao)
        TextView resultCountDownQihao;
        @Bind(R.id.ic_revealing2_id)
        ImageView icRevealing2Id;
        @Bind(R.id.result_revealing_text)
        TextView resultRevealingText;
        @Bind(R.id.result_revealing_countdown)
        TextView resultRevealingCountdown;
        @Bind(R.id.result_revealing_wrapper)
        LinearLayout resultRevealingWrapper;
        @Bind(R.id.caipiao_error)
        LinearLayout caipiaoError;
        @Bind(R.id.goods_free_label)
        ImageView goodsFreeLabel;
        @Bind(R.id.good_ten_label)
        ImageView goodTenLabel;
        @Bind(R.id.limit_duobao_id)
        ImageView limitDuobaoId;
        @Bind(R.id.custom_top_left_icon)
        ImageView customTopLeftIcon;

        public ReslutViewholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setinfo(GoodsItem item) {
            int postion = getAdapterPosition();
            if (postion >= 0) {
                GoodsItem layoutitem = mDatas.get(postion);
                if (item.getId().equals(layoutitem.getId()) && item.getQishu().equals(layoutitem.getQishu())) {
                    resultRevealingCountdown.setText(CountDownTimerManager.getInstance().transtime(item.getNew_time()));
                    if (item.getNew_time() == 0) {
                        resultRevealingText.setVisibility(View.VISIBLE);
                        resultRevealingCountdown.setVisibility(View.GONE);
                    }
                }
            }
        }
    }
}
